package com.hitqz.robot.biz.flow.component.oilSample;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.enums.TicketsFormFieldEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.DoorCtrlDto;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.LeftRightReachDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/10/4
 */
@LiteflowComponent(name = "[取油样机器人]取油柜-模组对接", value = "MathModuleComponent")
@RequiredArgsConstructor
@Slf4j
public class MathModuleComponent extends NodeComponent {
    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String deviceKey = "";
        for (String key : ticketsDto.getModalData().keySet()) {
            if (key.contains(TicketsFormFieldEnum.EQUIPMENT_LIST.getFieldName())) {
                deviceKey = key;
                break;
            }
        }
        String device = String.valueOf(ticketsDto.getModalData().get(deviceKey));
        RobotEquipment oilEquipment = robotEquipmentService.getById(Integer.valueOf(device));
        log.info("取油柜子id");
        List<RobotEquipment> carPlc = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                //.eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getType, EquipmentEnum.CAR_PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));
        RobotEquipment robotEquipment = carPlc.get(0);
        QueryParamsDto carDto = new QueryParamsDto();
        carDto.setPassword(robotEquipment.getPassword());
        carDto.setUsername(robotEquipment.getUsername());
        carDto.setPort(robotEquipment.getPort());
        carDto.setChannel(robotEquipment.getChannel());
        carDto.setIp(robotEquipment.getIp());
        IEquipmentService service =  equipmentFactory.getServiceByBrand(oilEquipment.getBrand());
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(oilEquipment.getPassword());
        queryParamsDto.setUsername(oilEquipment.getUsername());
        queryParamsDto.setPort(oilEquipment.getPort());
        queryParamsDto.setChannel(oilEquipment.getChannel());
        queryParamsDto.setIp(oilEquipment.getIp());
        DoorCtrlDto doorCtrlDto = new DoorCtrlDto();
        doorCtrlDto.setDoor(1);
        service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.START_MATCH, doorCtrlDto);
        // 查询对接信息
        // 转发信号

        while (true) {
            List<EquipmentParamsDto> equipmentParamsDtos =  service.getEquipmentParams(queryParamsDto);
            Optional<EquipmentParamsDto> oilEquipmentDto = equipmentParamsDtos.stream().filter(t-> t.getKey().equals("match")).findFirst();
            if (!oilEquipmentDto.isPresent()) {
                continue;
            }
            log.info("oilEquipmentDto:" + oilEquipmentDto.get().getValue());
            if (oilEquipmentDto.get().getValue().equals("1")) {
                break;
            }
            if (oilEquipmentDto.get().getValue().equals("2")) {
                log.info("对接失败");
                break;
            }
            List<EquipmentParamsDto> carParamsDto =  equipmentFactory.getServiceByBrand(robotEquipment.getBrand()).getEquipmentParams(carDto);
            // 转发信号
            Optional<EquipmentParamsDto> left = carParamsDto.stream().filter(t-> t.getKey().equals("left")).findFirst();
            Optional<EquipmentParamsDto> right = carParamsDto.stream().filter(t-> t.getKey().equals("right")).findFirst();
            if (right.isPresent() && left.isPresent()) {
                //log.info("Left {} and right {}", left.get().getValue(), right.get().getValue());
                LeftRightReachDto leftRightReachDto = new LeftRightReachDto();
                leftRightReachDto.setLeft(Integer.parseInt(left.get().getValue()));
                leftRightReachDto.setRight(Integer.parseInt(right.get().getValue()));
                service.executeCmd(queryParamsDto, false, EquipmentCmdEnum.LEFT_RIGHT, leftRightReachDto);
            }
        }



    }
}
